import sys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import SHPDriver
from selenium.webdriver.common.by import By
import time
import ddddocr
#商城首页测试用例
class  HomePage:
    def __init__(self):
        self.url="http://49.235.61.184/"
        self.driver=SHPDriver.driver
        self.driver.get(self.url)
    def captcha(self):
        ele = self.driver.find_element(By.XPATH, '//*[@id="form-verify-img"]')
        ele.screenshot('code.png')
        ele_bytes = ele.screenshot_as_png
        ocr = ddddocr.DdddOcr(show_ad=False)  # 关闭广告输出
        text = ocr.classification(ele_bytes)
        self.driver.find_element(By.XPATH, '//*[@type="text" and @name="verify"]').send_keys(text)
    # 测试首页(未登录)
    def HomePageTest1(self):
        # 测试网站LOGO是否存在
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/div/div[1]/a/img")
        # 测试个人中心是否存在
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[1]/div/a/span")
        # 测试"全部分类"按钮是否存在
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/div/div[1]/a/img")
        # 测试搜索框是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#search-input")
        # 商品分类栏目
        self.driver.find_element(By.CSS_SELECTOR, "#doc-topbar-collapse > ul > li.am-dropdown > a")
        # 我的商城
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-myself > div > a > span")
        # 我的收藏
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span")
        # 消息
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-message > div > a > span")
        # 购物车
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > span")  # 正确
        # 首页登录按钮
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[1]")
        # 注册按钮
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[2]")
        # 截图
        SHPDriver.GetScreenShot()

 # 首页功能测试(未登录)
    def HomePageTest2(self):
        current_method = sys._getframe().f_code.co_name
        wait = WebDriverWait(self.driver, 10)  # 全局显式等待实例

        # 1. 点击个人中心 → 刷新首页
        personal_center = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'个人中心')]"))
        )
        personal_center.click()
        time.sleep(2)
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()
        wait.until(EC.visibility_of_element_located((By.ID, "search-input")))  # 等待刷新后首页加载

        # 2. 点击我的商城 → 我的订单 → 刷新首页
        my_mall = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'我的商城')]"))
        )
        my_mall.click()
        my_order = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//p[contains(text(),'我的订单')]"))
        )
        my_order.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        wait.until(EC.visibility_of_element_located((By.ID, "search-input")))

        # 3. 点击我的收藏 → 商品收藏 → 刷新首页
        my_favor = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'我的收藏')]"))
        )
        my_favor.click()
        favor_item = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//p[contains(text(),'商品收藏')]"))
        )
        favor_item.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        wait.until(EC.visibility_of_element_located((By.ID, "search-input")))

        # 4. 点击更多入口 → 订单 → 刷新首页
        more_entry = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'更多')]"))
        )
        more_entry.click()
        order_item = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//p[contains(text(),'订单') and position()=2]"))
        )
        order_item.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        time.sleep(2)

        # 5. 点击购物车 → 刷新首页
        cart = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'购物车')]"))
        )
        cart.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        time.sleep(2)
        self.driver.refresh()  # 刷新
        time.sleep(2)

        # 6. 点击消息 → 刷新首页
        message = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'消息')]"))
        )
        message.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        time.sleep(2)

        # 回到首页（直接访问首页URL，确保页面状态纯净）
        self.driver.get(self.url)  # self.url是初始化时定义的首页地址，直接跳转
        time.sleep(2)
        # 7. 搜索框输入"手机"
        search_input = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='search-input']")))
        search_input.send_keys("手机")
        search_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='ai-topsearch']/span")))
        search_btn.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        time.sleep(2)
 #登录状态
    def HomePageTest3(self):
        #先登录
        self.driver.find_element(By.XPATH,"/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[1]").click()
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/div/div[2]/div[2]/div[2]/div/div[1]/div[1]/form/div[1]/input").send_keys("zzz111")
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/div/div[2]/div[2]/div[2]/div/div[1]/div[1]/form/div[2]/input").send_keys("zzz123")
        self.captcha()
        self.driver.find_element(By.XPATH,
                                 "/html/body/div[1]/div[1]/div[3]/div/div[2]/div[2]/div[2]/div/div[1]/div[1]/form/div[4]/button").click()
        time.sleep(2)
        current_method = sys._getframe().f_code.co_name
        wait = WebDriverWait(self.driver, 10)  # 全局显式等待实例
        # 点击个人中心跳转个人中心页面
        personal_center = wait.until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[1]/div/a/span"))
        )
        personal_center.click()
        time.sleep(2)
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        # 点击我的商城我的订单跳转订单管理页面
        my_mall = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'我的商城')]"))
        )
        my_mall.click()
        my_order = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//p[contains(text(),'我的订单')]"))
        )
        my_order.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        wait.until(EC.visibility_of_element_located((By.ID, "search-input")))
        # 点击我的收藏 → 商品收藏 → 刷新首页
        my_favor = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'我的收藏')]"))
        )
        my_favor.click()
        favor_item = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//p[contains(text(),'商品收藏')]"))
        )
        favor_item.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        wait.until(EC.visibility_of_element_located((By.ID, "search-input")))
        # 点击更多入口 → 订单 → 刷新首页
        more_entry = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'更多')]"))
        )
        more_entry.click()
        order_item = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//p[contains(text(),'订单') and position()=2]"))
        )
        order_item.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        time.sleep(2)

        # 点击购物车 → 刷新首页
        cart = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'购物车')]"))
        )
        cart.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        time.sleep(2)
        self.driver.refresh()  # 刷新
        time.sleep(2)

        # 点击消息 → 刷新首页
        message = wait.until(
            EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'消息')]"))
        )
        message.click()
        SHPDriver.GetScreenShot(custom_name=current_method)
        self.driver.refresh()  # 刷新
        time.sleep(2)







# 执行测试
Test = HomePage()
Test.HomePageTest2()
